<template>
  <div class="login">
    <van-nav-bar
      title="志愿者登录"
      left-text="返回"
      right-text="注册"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="logo_img">
      <img src="/pic&video/logo.png" alt="" />
    </div>
    <div class="login_body">
      <van-cell-group>
        <van-field
          v-model="value"
          label="用户名"
          placeholder="请输入用户名"
          left-icon="friends-o"
        />
        <van-field
          v-model="pwd"
          label="密码"
          placeholder="请输入用户名"
          left-icon="closed-eye"
          type="password"
        />
      </van-cell-group>
    </div>
    <van-button type="info" block round style="margin-top: 8vh" @click="enter"
      >登录</van-button
    >
  </div>
</template>

<script>
export default {
  
  data() {
    return {
      value: "",
      pwd: "",
    }
  },
  mounted(){
    this.userCheck()
  },

  methods: {
    userCheck(){
      if (window.sessionStorage.getItem("name")){
        this.$router.push("/mine")
      }
    },
 
    enter() {
      if (!this.value || !this.pwd) {
        this.$toast("输入框不能为空")
        return
      }
      let params = `username=${this.value}&password=${this.pwd}`
      this.axios.post("/login", params).then((res) => {
        if (res.data.code == 200) {
        // console.log(res.data.result)
        // this.$store.commit('saveUserData',res.data.result)
            window.sessionStorage.setItem("name", this.value)
          this.$router.push("/")
        } else {
          this.$toast("密码错误")
        }
      })
      
    },
    onClickLeft() {
      this.$router.push("/")
    },
    onClickRight() {
      this.$router.push("/register")
    },
  },
}
</script>

<style scoped>
.logo_img {
  width: 40%;
  margin: auto;
  margin-top: 15vh;
}
.login img {
  width: 100%;
}
.login_body {
  margin-top: 5vh;
}
</style>
